import QtQuick 2.12
import org.ukui.appwidget 1.0


AppWidget {
    id:appWidget
    appname:"weather"
    property real scaling: parent.width==0 ? 1 : parent.width / 448
    property real fontScaling: 1
    property real cardWidth : 448 * scaling
    property real cardHeight : 162 * scaling
    property real temperatureWidth : 98 * scaling
    property real temperatureHeight : 70 * scaling
    property real temperatureFontPixSize : 64 * scaling * ((fontScaling - 1) / 3 + 1) //以正常字体缩放幅度的三分之一进行温度字体的缩放
    property real weatherInfoHeight : 22 * scaling
    property real weatherInfoSpace : 16 * scaling
    property real weatherInfoFontPixSize : 16 * scaling * fontScaling
    property real weatherInfoTopSpace : 12 * scaling
    property real leftSpace : 24 * scaling
    property real topSpace : 12 * scaling
    property string fontColor : "white"
    property string gsettingPath: "org.kylin-weather.settings"
    property string fontGsettingPath: "org.ukui.style"
    property string fontGsettingKey: "systemFontSize"
    property string interfaceKey: "weather"
    property string imageDirPath:"file:///usr/share/kylin-weather/weather-card/background/"
    property string appWidgetStatusDefault
    Rectangle{
        width: cardWidth
        height: cardHeight
        color: "transparent"
        //背景图
        Image {
            id: weatherBackground
            anchors.fill: parent
            source: imageDirPath+"100.png"
        }
        //温度
        Text {
//            Rectangle{anchors.fill: parent;color: "red";opacity: 0.5}font.underline: true;font.overline: true
            id: temperature
            x:leftSpace
            y:topSpace
            width:temperatureWidth
            height:temperatureHeight
            text: qsTr("24°")
            font.pixelSize: temperatureFontPixSize
            color: fontColor
        }
        //城市天气信息
        Text {
            id: weatherInfoCity
            x:leftSpace
            y:temperature.y + temperature.height + weatherInfoTopSpace
            height:weatherInfoHeight
            text: qsTr("长沙市")
            font.pixelSize: weatherInfoFontPixSize
            color: fontColor
        }
        Text {
            id: weatherInfoType
            x:leftSpace
            y:weatherInfoCity.y + weatherInfoHeight + weatherInfoTopSpace
            height:weatherInfoHeight
            text: qsTr("晴天")
            font.pixelSize: weatherInfoFontPixSize
            color: fontColor
        }
        Text {
            id: weatherInfoHumidity
            x:weatherInfoType.x + weatherInfoType.width + weatherInfoSpace
            y:weatherInfoType.y
            height:weatherInfoHeight
            text: qsTr("湿度98%")
            font.pixelSize: weatherInfoFontPixSize
            color: fontColor
        }
        Text {
            id: weatherInfoWind
            x:weatherInfoHumidity.x + weatherInfoHumidity.width + weatherInfoSpace
            y:weatherInfoType.y
            height:weatherInfoHeight
            text: qsTr("东南风一级")
            font.pixelSize: weatherInfoFontPixSize
            color: fontColor
        }
        Rectangle{
            id:test
            x:temperature.x + temperature.width
            y:temperature.y
            width:temperatureWidth
            height:temperatureHeight
            color: fontColor
            visible: false
        }
        //简单逻辑
        Item {
            id:logic
            property string gsettingDataStr
            function updateUI (gsettingData){
                gsettingDataStr = gsettingData
                var list = gsettingDataStr.split(',')
                if (list.length < 9) {
                    return noDateUI()
                }
                var datetime = list[0]
                if (datetime.length !== 16) {
                    return noDateUI()
                }
                datetime = new Date(datetime)
                var nowtime = new Date()
                 //1000毫秒=1秒  60秒=1分钟  60分钟=1小时
                var interval = (nowtime - datetime)/1000/60/60
                if (interval > 2) {
                    // return noDateUI()
                }
                temperature.visible = true
                weatherInfoCity.visible = true
                weatherInfoType.visible = true
                weatherInfoHumidity.visible = true
                weatherInfoWind.visible = true
                temperature.text = list[5]
                weatherInfoCity.text = list[2]
                weatherInfoType.text = list[3]
                weatherInfoHumidity.text = "湿度" + list[4]
                weatherInfoWind.text = list[6] + list[7]
                weatherBackground.source = imageDirPath+list[8]+".png"
                if (weatherBackground.status == Image.Error) {
                    weatherBackground.source = imageDirPath + "100.png"
                }
            }
            function noDateUI (){
                temperature.visible = false
                weatherInfoCity.visible = false
                weatherInfoType.visible = false
                weatherInfoHumidity.visible = false
                weatherInfoWind.visible = false
                weatherBackground.source = imageDirPath+"N-A.png"
            }
            function updateFontUi(fontSize){
                appWidget.fontScaling = Number(fontSize) / 11
            }
        }
        //点击事件
        MouseArea{
            id:mouseArea
            anchors.fill: parent
            propagateComposedEvents: true
            onClicked: {
                appWidget.qmlclickedsignal(interfaceKey,"")
            }
            onPressAndHold: {
                mouse.accepted = false
            }
        }
        //绑定事件
        Component.onCompleted: {
            //天气信息更新
            creatGsetting(gsettingPath)
            gsettingUpdate.connect(onGsettingUpdate)
            logic.updateUI(getGsettingValue(gsettingPath,interfaceKey))
            //系统字号改变
            creatGsetting(fontGsettingPath)
            gsettingUpdate.connect(onFontGsettingUpdate)
            logic.updateFontUi(getGsettingValue(fontGsettingPath,fontGsettingKey))
            //监听小插件管理器状态
            appWidgetStatusDefault = appWidget.userstatus
            userStatusChangedSignal.connect(onUserStatusChanged)
        }
        function onGsettingUpdate(key, value){
            if (key === interfaceKey) {
                logic.updateUI(value)
            }
        }
        function onFontGsettingUpdate(key, value){
            if (key === fontGsettingKey) {
                logic.updateFontUi(value)
            }
        }
        function onUserStatusChanged(status){
            if (status === appWidgetStatusDefault) {
                mouseArea.enabled = true
            } else {
                mouseArea.enabled = false
            }
        }
    }
}



